window.onload=function () {
    var url = '/shop-user/index';
    var config = {
        elem: '#test'
        ,url:url
        ,cols: [[
            {field:'id', width:80, title: '用户id', sort: true}
            ,{field:'nickname', width:120, title: '昵称'}
            ,{field:'avatarurl', width:80, title: '头像', sort: true, templet:'#header'}
            ,{field:'order_num', width:80, title: '订单量', sort: true}
            ,{field:'mobile', width:120, title: '手机号'}
            ,{field:'address', title: '用户地址', minWidth: 150}
            ,{field:'shop_id', title: '是否绑定商家', width: 120,templet: function(d){
                var checked = d.shop_id ? 'checked' : '';
                return '<input type="checkbox" name="bindshop" lay-skin="switch" lay-text="是|否" '+checked+' disabled>'
            }}
            ,{field:'shop_name', title: '商家名称', width: 150,templet:"#shop"}
            ,{fixed: 'right', width:120, title:'操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器

        ]]
        ,page: true,
    };
    layui.use(['form','layer','jquery','table'], function(){
        var table = layui.table,
            form = layui.form,
            layer = layui.layer;
            $ = layui.jquery;

        table.render(config);
        //监听工具条
        table.on('tool(barDemo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if(layEvent === 'detail'){ //查看
                location.href = '/shop-user/info?id='+data.id;
            }else if(layEvent === 'shopdetail'){
                location.href = '/shop/get-info?id='+data.shop_id+'&type=1';
            }
        });

        //搜索
        form.on("submit(serch-form)",function(data){
            var user_name = $("input[name='user_name']").val();
            var url = '/shop-user/index?user_name='+ user_name;
            config.url = url;
            table.render(config);
            return false;

        });

    });
};